﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using Microsoft.WindowsMobile.Forms;
using System.IO;


namespace Coding4FunCamera
{
    public partial class Coding4FunCameraForm : Form
    {

        private const string _cameraDefaultPictureExtension = ".jpg";

        public Coding4FunCameraForm()
        {
            InitializeComponent();
        }
        private void Coding4FunCameraForm_Load(object sender, EventArgs e)
        {
            PictureDirectoryPath.Text = DirectoryBrowser.PersonalDirectory;
            textWidth.Text = Coding4FunCamera.Properties.Resources.WidthResolution;
            textHeight.Text = Coding4FunCamera.Properties.Resources.HeightResolution;
            textDefaultFileName.Text = Coding4FunCamera.Properties.Resources.DefaultFileName;
            btnSetResolution.Text = Coding4FunCamera.Properties.Resources.EditButtonText;
            radioColor.Checked = true;
            StillQualitySelection.SelectedItem = "Default";

            if (!IsCameraEnabled())
            {
                Application.Exit();
            }

        }

        private void btnSetPictureDirectory_Click(object sender, EventArgs e)
        {
            DirectoryBrowser directory = new DirectoryBrowser();
            directory.ShowDialog();
            if (directory.DialogResult == DialogResult.OK 
                &&  directory.SelectedDirectory != string.Empty)
            {
                PictureDirectoryPath.Text = directory.SelectedDirectory;
            }
        }

        private void menuExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuCameraDialog_Click(object sender, EventArgs e)
        {
            TakeStillPicture();
        }

        protected CameraCaptureStillQuality StillQuality
        {
            get
            {
                return (CameraCaptureStillQuality)StillQualitySelection.SelectedIndex;
            }
        }
       

        private  void TakeStillPicture()
        {
            CameraCaptureDialog cameraCapture = new CameraCaptureDialog();
            cameraCapture.StillQuality = this.StillQuality;
            cameraCapture.Owner = this;
            cameraCapture.DefaultFileName = textDefaultFileName.Text;
            cameraCapture.InitialDirectory = PictureDirectoryPath.Text;
            cameraCapture.Resolution = new Size(Convert.ToInt32(textWidth.Text), Convert.ToInt32(textHeight.Text));
            
            //We create a temp file name to see can create the final picture with the filter applied 
            if (!cameraCapture.DefaultFileName.ToLower().EndsWith(_cameraDefaultPictureExtension))
            {
                cameraCapture.DefaultFileName = cameraCapture.DefaultFileName + "___TEMP" + _cameraDefaultPictureExtension;
            }
            else
            {
                cameraCapture.DefaultFileName.Replace(_cameraDefaultPictureExtension, "___TEMP" + _cameraDefaultPictureExtension);
            }


            try
            {
                // Displays the "Camera Capture" dialog box
                if (DialogResult.OK == cameraCapture.ShowDialog())
                {
                    string fileName = cameraCapture.FileName.Replace("___TEMP", "");
                    ICameraFilter filter = GetPictureFilter(cameraCapture.FileName);
                    string saveFileName = CameraFileUtilities.incrementFileNameNumber(fileName);
                    filter.Apply().Save(saveFileName, ImageFormat.Jpeg);
                    File.Delete(cameraCapture.FileName);
                    // The method completed successfully.
                    MessageBox.Show("The picture has been successfully captured and saved to:\n\n" + saveFileName,
                        this.Text, MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                }
            }
            catch (ArgumentException ex)
            {
                // An invalid argument was specified.
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
            }
            catch (OutOfMemoryException ex)
            {
                // There is not enough memory to save the image or video.
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK,
                    MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
            catch (InvalidOperationException ex)
            {
                // An unknown error occurred.
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK,
                    MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
            catch (System.Exception ex)
            {
                // An unknown error occurred.
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK,
                    MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
            }
        }
        public ICameraFilter GetPictureFilter(string fileName)
        {

            if (radioColor.Checked)
            {
                return new ColorFilter(fileName);
            }
            else if (radioBW.Checked)
            {
                return new BlackAndWhiteFilter(fileName);
            }
            else //radioSepia.Checked
            {
                return new SepiaFilter(fileName);
            }
            
        }

        private bool IsCameraEnabled()
        {
            object cameraEnabled = Microsoft.WindowsMobile.Status.SystemState.GetValue(Microsoft.WindowsMobile.Status.SystemProperty.CameraEnabled);
            if (null != cameraEnabled && 0 == (int)cameraEnabled)
            {
                MessageBox.Show("The camera is disabled", this.Text,
                        MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                return false;
            }
            else
            {
                return true;
            }
        }

        private void btnSetResolution_Click(object sender, EventArgs e)
        {
            if (textWidth.ReadOnly)
            {
                btnSetResolution.Text = Coding4FunCamera.Properties.Resources.ApplyEditingButtonText;
                textHeight.ReadOnly = false;
                textWidth.ReadOnly = false;
            }
            else
            {
                btnSetResolution.Text = Coding4FunCamera.Properties.Resources.EditButtonText;
                textHeight.ReadOnly = true;
                textWidth.ReadOnly = true;
            }

        }

        private void btnSetName_Click(object sender, EventArgs e)
        {
            if (textDefaultFileName.ReadOnly)
            {
                textDefaultFileName.ReadOnly = false;
                btnSetName.Text = Coding4FunCamera.Properties.Resources.ApplySetFileName;
            }
            else
            {
                textDefaultFileName.ReadOnly = true;
                btnSetName.Text = Coding4FunCamera.Properties.Resources.SetFileName;
            }
        }

        private void Coding4FunCameraForm_KeyDown(object sender, KeyEventArgs e)
        {

        }

        private void menuBrowsePictures_Click(object sender, EventArgs e)
        {
            try
            {
                BrowsePictures browser = new BrowsePictures(PictureDirectoryPath.Text);
                browser.ShowDialog();
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void textWidth_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar) 
                & (Keys)e.KeyChar != Keys.Back 
                & (Keys)e.KeyChar != Keys.Delete)
            {
                e.Handled = true;
            } 

        }

        private void textHeight_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsNumber(e.KeyChar)
                & (Keys)e.KeyChar != Keys.Back
                & (Keys)e.KeyChar != Keys.Delete)
            {
                e.Handled = true;
            } 
        }

    }
}